package lifecycle;

import java.io.FileInputStream;
import java.io.IOException;
import java.net.MalformedURLException;
import java.util.HashMap;
import java.util.Hashtable;
import java.util.Iterator;
import java.util.Properties;

import javax.management.MBeanServerConnection;
import javax.management.MalformedObjectNameException;
import javax.management.ObjectName;
import javax.management.remote.JMXConnector;
import javax.management.remote.JMXConnectorFactory;
import javax.management.remote.JMXServiceURL;
import javax.naming.Context;

public class ServerHealthMonitoring extends AbstractJMX {

	/**
	 * @param args
	 */
	private static MBeanServerConnection connection;
	private static JMXConnector connector;
	private static final ObjectName service;
	private static String combea = "com.bea:Name=";
	private static String service1 = "DomainRuntimeService,Type=weblogic.management.mbeanservers.domainruntime.DomainRuntimeServiceMBean";
	private static String service2 = "RuntimeService,Type=weblogic.management.mbeanservers.runtime.RuntimeServiceMBean";

	static {
		try {
			service = new ObjectName(combea + service1);
		} catch (MalformedObjectNameException e) {
			throw new AssertionError(e.getMessage());
		}
	}

	public static void initConnection(String hostname, String portString,
			String username, String password) throws IOException,
			MalformedURLException {
		String protocol = "t3";
		Integer portInteger = Integer.valueOf(portString);
		int port = portInteger.intValue();
		String jndiroot = "/jndi/";
		String mserver = "weblogic.management.mbeanservers.domainruntime";
		JMXServiceURL serviceURL = new JMXServiceURL(protocol, hostname, port,
				jndiroot + mserver);
		Hashtable h = new Hashtable();
		h.put(Context.SECURITY_PRINCIPAL, username);
		h.put(Context.SECURITY_CREDENTIALS, password);
		h.put(JMXConnectorFactory.PROTOCOL_PROVIDER_PACKAGES,
				"weblogic.management.remote");
		connector = JMXConnectorFactory.connect(serviceURL, h);
		connection = connector.getMBeanServerConnection();
	}

	public static ObjectName[] getServerRuntimes() throws Exception {
		return (ObjectName[]) connection
				.getAttribute(service, "ServerRuntimes");
	}

	public void printNameAndState(String env) throws Exception {

		ObjectName arr[] = getServerRuntimes();
		// for (ObjectName temp : arr)
		// System.out.println("\n\t servers: " + temp);
		ObjectName domain = (ObjectName) connection.getAttribute(service,
				"DomainConfiguration");
		// System.out.println("Domain: " + domain.toString());
		ObjectName[] servers = (ObjectName[]) connection.getAttribute(domain,
				"Servers");
		for (ObjectName server : servers) {
			String aName = (String) connection.getAttribute(server, "Name");
			try {
				ObjectName ser = new ObjectName("com.bea:Name=" + aName
						+ ",Location=" + aName + ",Type=ServerRuntime");
				String serverState = (String) connection.getAttribute(ser,
						"State");
				System.out.println("\n\t Server: " + aName + "\t State: "
						+ serverState);
			} catch (javax.management.InstanceNotFoundException e) {
				System.out.println("\n\t Server: " + aName
						+ "\t State: SHUTDOWN (or Not Reachable)");
			}
		}
	}

	public void doLoop(String username, String password, String portString,
			HashMap asServers) throws Exception {

		for (Iterator iterator = asServers.keySet().iterator(); iterator
				.hasNext();) {
			String env = (String) iterator.next();
			String asHost = (String) asServers.get(env);
			System.out
					.println("-----------------------------------------------------------------------------");
			System.out.println("-" + env);
			System.out
					.println("-----------------------------------------------------------------------------");
			ServerHealthMonitoring as = new ServerHealthMonitoring();

			try {
				initConnection(asHost, portString, username, password);
			} catch (Exception e) {
				System.out.println("Error connecting to admin server");
			}
			as.printNameAndState(env);
			connector.close();
		}
	}

	public HashMap getHostNames() {
		HashMap asHostsMap = new HashMap();
		
		HashMap asUser = new HashMap();
		HashMap asPassword = new HashMap();

		asHostsMap.put("DEV1", "cgappt38.qcorpaa.aa.com");
		asHostsMap.put("DEV2", "cgappt40.qcorpaa.aa.com");
		asHostsMap.put("DEV-INT", "cgappt41.qcorpaa.aa.com");
		asHostsMap.put("TEST", "cgappt29.qcorpaa.aa.com");
		// create an instance of properties class

		Properties props = new Properties();

		// try retrieve data from file
		try {

			props.load(new FileInputStream("servers.properties"));
			
			String dev1 = props.getProperty("dev1");
			String dev1_user = props.getProperty("dev1.user");
			String dev1_pw = props.getProperty("dev1.password");
		}

		// catch exception in case properties file does not exist

		catch (IOException e) {
			System.out.println("servers.properties not found!!!");
		}
		
		return asHostsMap;
	}

	public static void main(String[] args){
		String asNamesArr[] = { "cgappt38.qcorpaa.aa.com",
				"cgappt40.qcorpaa.aa.com", "cgappt41.qcorpaa.aa.com",
				"cgappt29.qcorpaa.aa.com" };

		HashMap asHostsMap = new HashMap();
		asHostsMap.put("DEV1", "cgappt38.qcorpaa.aa.com");
		asHostsMap.put("DEV2", "cgappt40.qcorpaa.aa.com");
		asHostsMap.put("DEV-INT", "cgappt41.qcorpaa.aa.com");
		asHostsMap.put("TEST", "cgappt29.qcorpaa.aa.com");

		String portString = "7005";
		String username = "weblogic";
		String password = "weblogic";

		ServerHealthMonitoring healthMon = new ServerHealthMonitoring();
		try{
			healthMon.doLoop(username, password, portString, asHostsMap);
		}catch(Exception e){
			
		}
		
		// Stage and prod
		asHostsMap.clear();
		asHostsMap.put("STAGE", "cgwdms07.pdc.aa.com");
		asHostsMap.put("PROD", "cgwdmp06.tul.aa.com");
		password = "W@blogic786";
		try {
			healthMon.doLoop(username, password, portString, asHostsMap);
		} catch (Exception e) {
			
		}

	}
}
